/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class PropertyComparator implements Comparator<Object> {

	private List<SortCondition> objList;

	public PropertyComparator(List<SortCondition> objList) {
		this.objList = objList;
	}

	/**
	 * @param bool true升序 false降序
	 * @param mothodName 排序的字段的get方法名
	 */
	public PropertyComparator(boolean bool, String mothodName) {
		SortCondition sortCondition = new SortCondition();
		sortCondition.setBeSortPropertyMethodName(mothodName);
		sortCondition.setSortFlag(bool);
		List<SortCondition> list = new ArrayList<SortCondition>();
		list.add(sortCondition);
		this.objList = list;
	}

	private int sortDown(Date o1Date, Date o2Date) {
		if (o1Date == null || o2Date == null) {
			return o1Date==null?-1:1;
		}
		if (o1Date.after(o2Date)) {
			return 1;
		} else if (o2Date.after(o1Date)) {
			return -1;
		}
		return 0;
	}

	private int sortUp(Date o1Date, Date o2Date) {
		if (o1Date == null || o2Date == null) {
			return o1Date==null?1:-1;
		}
		if (o1Date.after(o2Date)) {
			return -1;
		} else if (o2Date.after(o1Date)) {
			return 1;
		}
		return 0;
	}

	public int compare(Object o1, Object o2) {
		try {
			for (SortCondition sortPrinciple : this.objList) {
				Method method1 = o1.getClass().getMethod(
						sortPrinciple.getBeSortPropertyMethodName(),
						new Class[] {});
				Method method2 = o2.getClass().getMethod(
						sortPrinciple.getBeSortPropertyMethodName(),
						new Class[] {});
				if (Date.class.isInstance(method1.invoke(o1))) {
					Date o1Date = (Date) method1.invoke(o1);
					Date o2Date = (Date) method2.invoke(o2);
					int i = 0;
					if (sortPrinciple.getSortFlag()) {
						i = sortUp(o1Date, o2Date);
						if (i != 0) {
							return i;
						}
					} else {
						i = sortDown(o1Date, o2Date);
						if (i != 0) {
							return i;
						}
					}
				} else if (String.class.isInstance(method1.invoke(o1))) {
					String str1 = (String) method1.invoke(o1);
					String str2 = (String) method2.invoke(o2);
					if (str1 != null && str2 != null) {
						if (sortPrinciple.getSortFlag()
								&& str1.compareTo(str2) < 0) {
							return -1;
						} else if (sortPrinciple.getSortFlag()
								&& str1.compareTo(str2) > 0) {
							return 1;
						} else if (str1.compareTo(str2) < 0) {
							return 1;
						} else if (str1.compareTo(str2) > 0) {
							return -1;
						}
					} else if (str1 != null && str2 == null) {
						if (sortPrinciple.getSortFlag()) {
							return 1;
						} else {
							return -1;
						}
					} else if (str1 == null && str2 != null) {
						if (sortPrinciple.getSortFlag()) {
							return -1;
						} else {
							return 1;
						}
					}
				} else if (Long.class.isInstance(method1.invoke(o1))) {
					Long Long1 = (Long) method1.invoke(o1);
					Long Long2 = (Long) method2.invoke(o2);
					if (Long1 != null && Long2 != null) {
						if (sortPrinciple.getSortFlag() && Long1 < Long2) {
							return -1;
						} else if (sortPrinciple.getSortFlag() && Long1 > Long2) {
							return 1;
						} else if (Long1 < Long2) {
							return 1;
						} else if (Long1 > Long2) {
							return -1;
						}
					} else if (Long1 != null && Long2 == null) {
						if (sortPrinciple.getSortFlag()) {
							return 1;
						} else {
							return -1;
						}
					} else if (Long1 == null && Long2 != null) {
						if (sortPrinciple.getSortFlag()) {
							return -1;
						} else {
							return 1;
						}
					}
				}else if (Integer.class.isInstance(method1.invoke(o1))) {
					Integer i1 = (Integer) method1.invoke(o1);
					Integer i2 = (Integer) method2.invoke(o2);
					if (i1 != null && i2 != null) {
						if (sortPrinciple.getSortFlag() && i1 < i2) {
							return -1;
						} else if (sortPrinciple.getSortFlag() && i1 > i2) {
							return 1;
						} else if (i1 < i2) {
							return 1;
						} else if (i1 > i2) {
							return -1;
						}
					} else if (i1 != null && i2 == null) {
						if (sortPrinciple.getSortFlag()) {
							return 1;
						} else {
							return -1;
						}
					} else if (i1 == null && i2 != null) {
						if (sortPrinciple.getSortFlag()) {
							return -1;
						} else {
							return 1;
						}
					}
				}
			}
			return 0;
		} catch (Exception e) {
			return 0;
		}
	}

}
